@extends('layouts.header')
@section('title','对接系统管理')
@section('content')
<nav class="breadcrumb">
    <i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 基础信息 <span class="c-gray en">&gt;</span> 对接系统管理 <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray">
        <form id="form" class="form form-horizontal">
            {{csrf_form_field()}}
            <div class="row cl">
                <label class="form-label col-sm-3">名称：</label>
                <div class="formControls col-sm-7">
                    <input type="text" name="name" class="input-text required" value="鲁软风控系统" />
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">基础交互地址：</label>
                <div class="formControls col-sm-7">
                    <input type="text" name="base_url" class="input-text required" value="https://ip:port/" />
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">交互控制器：</label>
                <div class="formControls col-sm-7">
                    <input type="text" name="controller" class="input-text required" value="RequestGwDataController" />
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">上传方式：</label>
                <div class="formControls col-sm-7">
                    <label><input type="radio" name="type" class="required" value="0" checked>单样品</label>
                    <label class="ml-15"><input type="radio" name="type" class="required" value="1">批次</label>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">获取待检参数：</label>
                <div class="formControls col-sm-7">
                    <table class="table table-border table-bordered table-hover table-bg table-responsive">
                        <tr>
                            <th>名称</th>
                            <th>参数</th>
                            <th>必填 <br><b class="c-red">1:是；0否；默认0</b></th>
                        </tr>
                        <tr>
                            <td><input type="text" name="params[name][]" class="input-text" placeholder="如:开始时间"></td>
                            <td><input type="text" name="params[value][]" class="input-text" placeholder="如:start_time"></td>
                            <td><input type="text" name="params[must][]" class="input-text" placeholder="1:是；0：否；默认0"></td>
                        </tr>
                    </table>
                    <b class="btn btn-primary radius size-MINI " onclick="addParams(this)">&nbsp;&nbsp;+ &nbsp;&nbsp;</b>
                </div>
            </div>
            <div class="row cl text-c">
                <b class="btn btn-primary radius" onclick="add()">添加</b>
            </div>
        </form>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg table-responsive" id="datatable">
            <thead>
            <tr class="text-c">
                <th>名称</th>
                <th>基础交互地址</th>
                <th @if(!isAdmin()) hidden @endif>交互控制器</th>
                <th>获取待检参数</th>
                <th>试验中心所属市公司单位ID</th>
                <th>组织机构ID</th>
                <th style="width:30px;">扫码上传 <br>0：不可以 <br>1：可以</th>
                <th style="width:100px;">报告格式</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                @foreach($data['list'] as $item)
                    <tr class="text-c">
                        <td>{{$item->name}}</td>
                        <td><input type="text" name="base_url" value="{{$item->base_url}}" class="input-text"></td>
                        <td @if(!isAdmin()) hidden @endif><input type="text" name="controller" value="{{$item->controller}}" class="input-text"></td>
                        <td>
                            <div>
                                <table class="table table-border table-bordered table-hover table-bg table-responsive">
                                    <thead>
                                        <tr>
                                            <th>名称</th>
                                            <th>参数</th>
                                            <th>必填 <br><b class="c-red">1:是；0否</b></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @php $i=-1; $params = json_decode($item->params,true);   @endphp
                                        @if($params)
                                            @foreach($params as $name => $value)
                                                @php $i+=1; $values = explode('&&',$value); @endphp
                                                <tr>
                                                    <td><input type="text" name="params[{{$item->id}}][name][{{$i}}]" class="input-text" value="{{$name}}"></td>
                                                    <td><input type="text" name="params[{{$item->id}}][value][{{$i}}]" class="input-text" value="{{$values[0]}}"></td>
                                                    <td><input type="text" name="params[{{$item->id}}][must][{{$i}}]" class="input-text" value="{{array_get($values,1,0)}}"></td>
                                                </tr>
                                            @endforeach
                                        @else
                                            <tr>
                                                <td><input type="text" name="params[{{$item->id}}][name][0]" class="input-text"></td>
                                                <td><input type="text" name="params[{{$item->id}}][value][0]" class="input-text"></td>
                                                <td><input type="text" name="params[{{$item->id}}][must][0]" class="input-text" placeholder="默认0"></td>
                                            </tr>
                                        @endif
                                    </tbody>
                                    
                                </table>
                                <b class="btn btn-primary radius size-MINI f-r" onclick="addParams(this,{{$item->id}})">&nbsp;&nbsp;+ &nbsp;&nbsp;</b>
                            </div>
                        </td>
                        <td><input type="text" name="blgSgsId" value="{{$item->blgSgsId}}" class="input-text"></td>
                        <td><input type="text" name="org_id" value="{{$item->org_id}}" class="input-text"></td>
                        <td><input type="text" name="canScan" value="{{$item->canScan}}" class="input-text"></td>
                        <td>
                            @php $report_style = data_get($item,'report_style',1); @endphp
                            <select name="report_style" class="input-text">
                                <option value="1" @if($report_style == 1) selected @endif>excel</option>
                                <option value="2" @if($report_style == 2) selected @endif>pdf</option>
                            </select>
                        </td>
                        <td>
                            <a onclick="edit(this,'{{$item->id}}')" class="btn btn-primary size-MINI radius">保存修改</a>
                            <a onclick="del(this,'{{$item->id}}')" class="btn btn-danger size-MINI radius">删除</a>
                            <a onclick="layer_show('关系映射','map?third_id={{$item->id}}',0,0,true)" class="btn btn-primary size-MINI radius">映射关系</a>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>


<script type="text/javascript">
    var index;
    /*删除*/
    function del(obj,thirdId) {
        layer.confirm('确认要删除吗？', function (index) {
            ajax('post', 'del', {id: thirdId,_form_token:'{{csrf_form_token()}}'}, delBak);
            function delBak(data) {
                if (data == 1) {
                    $(obj).closest('tr').remove();
                    layer.msg('删除成功!', {icon: 1, time: 2000});
                } else {
                    layer.msg('删除失败!', {icon: 2, time: 2000});
                }
            }
        });
    }
    function add() {
        let form = $('#form');
        if (form.valid()) {
            ajax('post','add',form.serialize(),addBak,0);
            function addBak(res) {
                if(res == 1){
                    location.reload();
                    layer.msg('添加成功！');
                }else{
                    layer.msg(res);
                }
            }
        }
    }
    function edit(obj,thirdId) {
        let params = {
            id:thirdId,
            _form_token:'{{csrf_form_token()}}'
        };
        $(obj).closest('tr').find('input,select').each(function(){
            params[$(this).attr('name')] = $(this).val();
        })
        // console.log(params);
        ajax('post', 'edit', params);
    }

    function addParams(obj,third_id=0) {
        let tb = $(obj).closest('div').find('table'),
            i = $(tb).find('tbody').find('tr').length,
            params = "params";
        if (third_id != 0) {
            params += "["+third_id+"]";
        }
        let trStr = '<tr>'+
                        '<td><input type="text" name="'+params+'[name]['+i+']" class="input-text"></td>'+
                        '<td><input type="text" name="'+params+'[value]['+i+']" class="input-text"></td>'+
                        '<td><input type="text" name="'+params+'[must]['+i+']" class="input-text" placeholder="默认0"></td>'+
                    '</tr>';
        tb.append(trStr);
    }
</script>
@endsection